@typeparam TItem
@inherits BaseDataGridComponent
@* there are two CascadingValue because one is used only to define columns, and the other is to build rows based on those columns *@
@* This one is to build rows *@
<CascadingValue Value=this>
    <Table Class="@Class" Style="@Style" Margin="@Margin" Padding="@Padding" Striped="@Striped" Bordered="@Bordered" Borderless="@Borderless" Hoverable="@Hoverable" Narrow="@Narrow">
        <TableHeader>
            @if ( ShowCaptions )
            {
                <TableRow Class="@HeaderRowClass" Style="@HeaderRowStyle">
                    @foreach ( var column in DisplayableColumns )
                    {
                        @if ( column.ColumnType == DataGridColumnType.Command )
                        {
                            @if ( Editable )
                            {
                                <TableHeaderCell Class="@column.HeaderCellClass" Style="@column.HeaderCellStyle" width="@column.Width">
                                    @if ( CanInsertNewItem && editState != DataGridEditState.New )
                                    {
                                        <_DataGridNewCommand TItem="TItem" New="@OnNewCommand" />
                                    }
                                </TableHeaderCell>
                            }
                        }
                        else
                        {
                            <TableHeaderCell Class="@column.HeaderCellClass" Style="@($"{column.HeaderCellStyle}{(Sortable ? " cursor: pointer" : "")}")" Clicked="@(async () => await OnSortClicked( column ) )" width="@column.Width">
                                @if ( column.ShowCaption )
                                {
                                    @column.Caption
                                }
                                @if ( Sortable && column.Sortable && column.CurrentDirection != SortDirection.None )
                                {
                                    <Icon Name="@(column.CurrentDirection == SortDirection.Descending ? IconName.SortDown : IconName.SortUp)" />
                                }
                            </TableHeaderCell>
                        }
                    }
                </TableRow>
            }
            @if ( Filterable )
            {
                <TableRow Class="@FilterRowClass" Style="@FilterRowStyle">
                    @foreach ( var column in DisplayableColumns )
                    {
                        @if ( !column.Filterable )
                        {
                            <TableHeaderCell Class="@FilterRowClass" Style="@FilterRowStyle" width="@column.Width"></TableHeaderCell>
                            continue;
                        }

                        @if ( column.ColumnType == DataGridColumnType.Command )
                        {
                            @if ( Editable )
                            {
                                // since Clear Filter is located in the command column it can be visible onle when Edit is enabled
                                <TableHeaderCell Class="@column.FilterCellClass" Style="@column.FilterCellStyle" width="@column.Width">
                                    <_DataGridClearFilterCommand TItem="TItem" ClearFilter="@OnClearFilterCommand" />
                                </TableHeaderCell>
                            }
                        }
                        else
                        {
                            <TableHeaderCell Class="@FilterRowClass" Style="@FilterRowStyle" width="@column.Width">
                                @if ( column.FilterTemplate != null )
                                {
                                    @(column.FilterTemplate(column.FilterContext))
                                }
                                else
                                {
                                    <TextEdit Text="@column.Filter.SearchValue" TextChanged="@(async (newValue) => await OnFilterChanged(column, newValue))" />
                                }
                            </TableHeaderCell>
                        }
                    }
                </TableRow>
            }
        </TableHeader>
        <TableBody>
            @if ( Editable && editState == DataGridEditState.New && EditMode != DataGridEditMode.Popup )
            {
                <_DataGridRowEdit TItem="TItem" Item="@editItem" Columns="@Columns" CellValues="@editItemCellValues" Save="@OnSaveCommand" Cancel="@OnCancelCommand" EditMode="@EditMode" />
            }
            @foreach ( var item in DisplayData )
            {
                @if ( Editable && editState == DataGridEditState.Edit && EditMode != DataGridEditMode.Popup && (object)item == (object)editItem )
                {
                    <_DataGridRowEdit TItem="TItem" Item="@editItem" Columns="@Columns" CellValues="@editItemCellValues" Save="@OnSaveCommand" Cancel="@OnCancelCommand" EditMode="@EditMode" />
                }
                else
                {
                    <_DataGridRow @key="@item" TItem="TItem" Item="@item" Columns="@DisplayableColumns" HoverCursor="@(RowHoverCursor?.Invoke(item) ?? Cursor.Pointer)" Edit="@OnEditCommand" Delete="@OnDeleteCommand" Save="@OnSaveCommand" Cancel="@OnCancelCommand" Selected="@OnSelectedCommand" Clicked="@OnRowClickedCommand" DoubleClicked="@OnRowDoubleClickedCommand" Class="@RowClass?.Invoke(item)" Style="@RowStyle?.Invoke(item)" />
                    @if ( DetailRowTemplate != null )
                    {
                        var canShow = DetailRowTrigger?.Invoke( item ) ?? true;

                        @if ( canShow )
                        {
                            <_DataGridDetailRow TItem="TItem" Item="@item" Columns="@Columns">
                                @DetailRowTemplate( item )
                            </_DataGridDetailRow>
                        }
                    }
                }
            }
        </TableBody>
        @if ( HasAggregates )
        {
            <TableFooter>
                <_DataGridAggregateRow TItem="TItem" Aggregates="@Aggregates" Columns="@DisplayableColumns" Class="@GroupRowClass" Style="@GroupRowStyle" />
            </TableFooter>
        }
    </Table>
    @if ( ShowPager )
    {
        <Pagination>
            <PaginationItem Disabled="@(CurrentPage <= 1)">
                <PaginationLink Page="first" Clicked="@OnPaginationItemClick">
                    @if ( FirstPageButtonTemplate != null )
                    {
                        @FirstPageButtonTemplate
                    }
                    else
                    {
                        @("First")
                    }
                </PaginationLink>
            </PaginationItem>
            <PaginationItem Disabled="@(CurrentPage <= 1)">
                <PaginationLink Page="prev" Clicked="@OnPaginationItemClick">
                    @if ( PreviousPageButtonTemplate != null )
                    {
                        @PreviousPageButtonTemplate
                    }
                    else
                    {
                        @("Prev")
                    }
                </PaginationLink>
            </PaginationItem>
            @for ( int i = FirstVisiblePage; i <= LastVisiblePage; ++i )
            {
                var pageNumber = i;
                var pageNumberString = i.ToString();
                var pageActive = pageNumber == CurrentPage;

                <PaginationItem Disabled="@(pageNumber == CurrentPage)" Active="@pageActive">
                    <PaginationLink Page="@pageNumberString" Clicked="@OnPaginationItemClick">
                        @pageNumberString
                    </PaginationLink>
                </PaginationItem>
            }
            <PaginationItem Disabled="@(CurrentPage >= LastPage)">
                <PaginationLink Page="next" Clicked="@OnPaginationItemClick">
                    @if ( NextPageButtonTemplate != null )
                    {
                        @NextPageButtonTemplate
                    }
                    else
                    {
                        @("Next")
                    }
                </PaginationLink>
            </PaginationItem>
            <PaginationItem Disabled="@(CurrentPage >= LastPage)">
                <PaginationLink Page="last" Clicked="@OnPaginationItemClick">
                    @if ( LastPageButtonTemplate != null )
                    {
                        @LastPageButtonTemplate
                    }
                    else
                    {
                        @("Last")
                    }
                </PaginationLink>
            </PaginationItem>
        </Pagination>
    }
    @if ( editItem != null && EditMode == DataGridEditMode.Popup )
    {
        <_DataGridModal TItem="TItem"
                        EditItem="@editItem"
                        EditItemCellValues="@editItemCellValues"
                        Columns="@Columns"
                        PopupVisible="@PopupVisible"
                        EditState="@editState"
                        Save="@OnSaveCommand"
                        Cancel="@OnCancelCommand" />
    }
</CascadingValue>
@* This one is to hold information about columns *@
<CascadingValue Value=this>
    @DataGridAggregates
    @DataGridColumns
    @ChildContent
</CascadingValue>